﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PilotProject;
using PilotMVC.Models;
using System.Web.Security;
using System.Data;

namespace PilotMVC.Controllers
{
    [Authorize(Roles = "Admin")]
    public class ExaminerController : Controller
    {
        //
        // GET: /Examiner/

        private PilotEntities1 db = new PilotEntities1();


        public ActionResult Index(string jpa, string firstName, string lastName)
        {
            var examiners = from examiner in db.People.OfType<Examiner>() select examiner;
            var examinerView = new ManageExaminerView();


            if (!string.IsNullOrEmpty(jpa) && !jpa.Equals(""))
            {
                examinerView.Examiners = db.People.Include("Address").OfType<Examiner>().Where(e => e.JPA.Contains(jpa)).ToList();
            }
            if (!string.IsNullOrEmpty(firstName) && !firstName.Equals(""))
            {
                examinerView.Examiners = db.People.Include("Address").OfType<Examiner>().Where(e => e.FirstName.ToLower().Contains(firstName.ToLower())).ToList();
            }
            if (!string.IsNullOrEmpty(lastName) && !lastName.Equals(""))
            {
                examinerView.Examiners = db.People.Include("Address").OfType<Examiner>().Where(e => e.LastName.ToLower().Contains(lastName.ToLower())).ToList();
            }


            return View(examinerView);
        }



        //
        // GET: /Candiate/Create

        public ActionResult Create()
        {
            //ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Street1");
            Examiner examiner = new Examiner();
            examiner.Username = null;
            examiner.Password = null;
            examiner.DateOfBirth = DateTime.Now.Date;
            //return View(examiner);

            return View(examiner);
        }

        //
        // POST: /Candiate/Create

        [HttpPost]
        public ActionResult Create(Examiner examiner)//, FormCollection formCollection
        {


            if (ModelState.IsValid)
            {
                var address = new Address();
                address.Id = Guid.NewGuid();
                address.SuburbId = examiner.Address.SuburbId;
                address.Street1 = examiner.Address.Street1;
                address.Street2 = examiner.Address.Street2;
                db.Addresses.AddObject(address);


                var person = new Examiner();
                person.Id = Guid.NewGuid();
                person.DateOfBirth = examiner.DateOfBirth;
                person.Address = address;
                person.ARN = examiner.ARN;
                person.Email = examiner.Email;
                person.FirstName = examiner.FirstName;
                person.JPA = examiner.JPA;
                person.LastName = examiner.LastName;
                person.Telephone = examiner.Telephone;
                person.Mobile = examiner.Mobile;
                person.Username = examiner.Username;
                person.Password = examiner.Password;
                
                db.People.AddObject(person);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(examiner);
        }

        //
        // GET: /Candiate/Edit/5

        public ActionResult Edit(Guid id)
        {
            Examiner examiner = db.People.OfType<Examiner>().Include("Address").Single(c => c.Id == id);
            return View(examiner);
        }

        //
        // POST: /Candiate/Edit/5

        [HttpPost]
        public ActionResult Edit(Examiner examiner, FormCollection data, String DateOfBirth)//, FormCollection formCollection
        {
            ModelState.Remove("States");
            if (ModelState.IsValid)
            {
                var person = db.People.Where(p => p.Id == examiner.Id).First();
                person.Address.SuburbId = examiner.Address.SuburbId;
                person.Address.Street1 = examiner.Address.Street1;
                person.Address.Street2 = examiner.Address.Street2;
                person.Email = examiner.Email;
                person.FirstName = examiner.FirstName;
                person.DateOfBirth = examiner.DateOfBirth;
                person.LastName = examiner.LastName;
                person.Username = examiner.Username;
                person.Password = examiner.Password;
                person.Mobile = examiner.Mobile;
                person.Telephone = examiner.Telephone;
                person.ARN = examiner.ARN;
                person.JPA = examiner.JPA;
                db.SaveChanges();
                return RedirectToAction("Index", examiner);
            }
            ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Street1", examiner.AddressId);
            return View(examiner);
        }



        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

    }
}

